<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('任务分配')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <input type="hidden" name="templateId" id="templateId" th:value="${template.id}">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>定时描述：</label>
                                <input type="text" name="naIndex" th:value="${template.jobDesc}" disabled/>
                            </li>
                            <li>
                                <label>CRON：</label>
                                <input type="text" name="naIndex" th:value="${template.jobCron}" disabled/>
                            </li>
                            <li>
                                <label>表名：</label>
                                <input type="text" name="naFilter" id="naFilter" />
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                                <a class="btn btn-info btn-rounded btn-sm" onclick="toAllTaskExec()"><i class="fa fa-edit"></i>&nbsp;执行</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>
            <div class="tabs-container" style="margin-top: 80px">
                <ul class="nav nav-tabs" id="tabs">
                    <li class="active"><a data-toggle="tab" type="datax" href="#tab-1" aria-expanded="true">单表</a>
                    </li>
                    <li class=""><a data-toggle="tab" type="inter" href="#tab-2" aria-expanded="false">接口</a>
                    </li>
                    <li class=""><a data-toggle="tab" type="dw" href="#tab-3" aria-expanded="false">数仓</a>
                    </li>
                    <li class=""><a data-toggle="tab" type="dm" href="#tab-4" aria-expanded="false">集市</a>
                    </li>
                </ul>
                <div class="tab-content">
                    <div id="tab-1" class="tab-pane active">
                        <div class="panel-body">
                            <div class="btn-group-sm" id="toolbar" role="group">
                                <a class="btn btn-success" onclick="toAddDatax()">
                                    <i class="fa fa-plus"></i> 添加
                                </a>
                                <a class="btn btn-primary" onclick="toTaskExec()" >
                                    <i class="fa fa-edit"></i> 执行一次
                                </a>
                                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()">
                                    <i class="fa fa-remove"></i> 删除
                                </a>
                            </div>
                            <div class="col-sm-12 select-table table-striped">
                                <table id="bootstrap-table"></table>
                            </div>
                        </div>
                    </div>
                    <div id="tab-2" class="tab-pane">
                        <div class="panel-body">
                            <strong>移动设备优先</strong>
                            <p>在 Bootstrap 2 中，我们对框架中的某些关键部分增加了对移动设备友好的样式。而在 Bootstrap 3 中，我们重写了整个框架，使其一开始就是对移动设备友好的。这次不是简单的增加一些可选的针对移动设备的样式，而是直接融合进了框架的内核中。也就是说，Bootstrap 是移动设备优先的。针对移动设备的样式融合进了框架的每个角落，而不是增加一个额外的文件。</p>
                        </div>
                    </div>
                    <div id="tab-3" class="tab-pane">
                        <div class="panel-body">
                            <div class="btn-group-sm" role="group">
                                <a class="btn btn-success" onclick="toAddDw()">
                                    <i class="fa fa-plus"></i> 添加
                                </a>
                                <a class="btn btn-primary" onclick="toTaskExec()" >
                                    <i class="fa fa-edit"></i> 执行一次
                                </a>
                            </div>
                            <div class="col-sm-12 select-table table-striped">
                                <table id="dw-table"></table>
                            </div>
                        </div>
                    </div>
                    <div id="tab-4" class="tab-pane">
                        <div class="panel-body">
                            <div class="btn-group-sm" role="group">
                                <a class="btn btn-success" onclick="toAddDm()">
                                    <i class="fa fa-plus"></i> 添加
                                </a>
                                <a class="btn btn-primary" onclick="toTaskExec()" >
                                    <i class="fa fa-edit"></i> 执行一次
                                </a>
                            </div>
                            <div class="col-sm-12 select-table table-striped">
                                <table id="dm-table"></table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
     <div class="modal inmodal fade" id="myModal6" tabindex="-1" role="dialog" aria-hidden="true" >
         <div class="modal-dialog modal-lg">
             <div class="modal-content">
                 <div class="modal-header">
                     <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
                     </button>
                     <h4 class="modal-title">任务执行[<span style="color: red" id="execTypeName"></span>]</h4>
                 </div>
                 <div class="modal-body">
                     <input type="hidden" id="execType"/>
                     <div class="form-group" style="width: 90%">
                         <label class="col-sm-3 font-noraml is-required;" style="text-align-last: right;vertical-align: center">时间范围：</label>
                         <div class="col-sm-8">
                             <div class="input-daterange input-group">
                                 <input type="text" class="input-sm form-control" id="laydate-startTime" autocomplete="off" placeholder="yyyy-MM-dd"/>
                                 <span class="input-group-addon">到</span>
                                 <input type="text" class="input-sm form-control" id="laydate-endTime" autocomplete="off" placeholder="yyyy-MM-dd"/>
                             </div>
                         </div>
                     </div>
                 </div>
                 <div class="modal-footer">
                     <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
                     <button type="button" class="btn btn-primary" onclick="taskExec()">确定</button>
                 </div>
             </div>
         </div>
     </div>
    <th:block th:include="include :: footer" />
     <th:block th:include="include :: datetimepicker-js" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('system:template:edit')}]];
        var removeFlag = [[${@permission.hasPermi('system:template:remove')}]];
        var prefix = ctx + "base/job/template/task";
        function taskAllocation(id) {
            $.modal.openTab("模板任务分配","")
        }
        function toAllTaskExec() {
            $("#execType").val("all");
            $("#execTypeName").text("全部");
            $("#myModal6").modal("show");
        }
        function toTaskExec() {
            var execType = $("#tabs").find("li.active a").attr("type");
            $("#execTypeName").text(execType);
            $("#execType").val(execType);
            $("#myModal6").modal("show");
        }
        function toAddDatax() {
            $.modal.open("单表任务添加",prefix+"/toAddDatax?templateId="+$("#templateId").val());
        }
        function toAddDw() {
            $.modal.open("数仓任务添加",prefix+"/toAddDw?templateId="+$("#templateId").val());
        }
        function toAddDm() {
            $.modal.open("数仓任务添加",prefix+"/toAddDm?templateId="+$("#templateId").val());
        }
        function taskExec() {
            var type = $("#execType").val();
            var jobIds=[];
            if(type=="datax") {
                var data = $("#bootstrap-table").bootstrapTable('getSelections');
                $.each(data,function (index,item) {
                    jobIds.push(item.idDrawDatax);
                });
            } else if(type=="dw"){
                var data = $("#dw-table").bootstrapTable('getSelections');
                $.each(data,function (index,item) {
                    jobIds.push(item.idDw);
                });
            }  else if(type=="dm"){
                var data = $("#dm-table").bootstrapTable('getSelections');
                $.each(data,function (index,item) {
                    jobIds.push(item.idDrawDm);
                });
            }
            $.ajax({
                url: ctx+"base/job/trigger",
                cache: false,
                type: 'POST',
                dataType: 'json',
                data: JSON.stringify({"jobIds":jobIds,"type":type,"templateId":$("#templateId").val(),"dayBegin":$("#laydate-startTime").val(),"dayEnd":$("#laydate-endTime").val()}),
                contentType: 'application/json;charset=UTF-8',
                success: function (result) {
                    $.modal.closeLoading();
                    if(result.code==0) {
                        $.modal.alertSuccess('执行完毕，请通过调度日志查看！');
                        $("#myModal6").modal("hide");
                    } else {
                        $.modal.alertError(result.msg);
                    }
                }
            });
            $.modal.loading("任务执行中,请稍后");
        }
        function remove(idFk,type) { // type: datax,dw,dm
            $.ajax({
                url: prefix+"/remove2",
                type: 'POST',
                dataType: 'json',
                data: JSON.stringify({"idFk":idFk,"idTemplate":$("#templateId").val(),"euType":type}),
                contentType: 'application/json;charset=UTF-8',
                success: function (result) {
                    if(result.code==0) {
                        if(type=="datax") {
                            $("#bootstrap-table").bootstrapTable("refresh");
                        } else if(type=="dw") {
                            $("#dw-table").bootstrapTable("refresh");
                        } else if(type=="dm") {
                            $("#dm-table").bootstrapTable("refresh");
                        }
                    } else {
                        $.modal.alertError(result.msg);
                    }
                }
            });
        }
        $(function() {
            layui.use('laydate', function() {
                var laydate = layui.laydate;
                var startDate = laydate.render({
                    elem: '#laydate-startTime',
                    max: $('#laydate-endTime').val(),
                    theme: 'molv',
                    trigger: 'click',
                    done: function (value, date) {
                        // 结束时间大于开始时间
                        if (value !== '') {
                            endDate.config.min.year = date.year;
                            endDate.config.min.month = date.month - 1;
                            endDate.config.min.date = date.date;
                        } else {
                            endDate.config.min.year = '';
                            endDate.config.min.month = '';
                            endDate.config.min.date = '';
                        }
                    }
                });
                var endDate = laydate.render({
                    elem: '#laydate-endTime',
                    min: $('#laydate-startTime').val(),
                    theme: 'molv',
                    trigger: 'click',
                    done: function (value, date) {
                        // 开始时间小于结束时间
                        if (value !== '') {
                            startDate.config.max.year = date.year;
                            startDate.config.max.month = date.month - 1;
                            startDate.config.max.date = date.date;
                        } else {
                            startDate.config.max.year = '';
                            startDate.config.max.month = '';
                            startDate.config.max.date = '';
                        }
                    }
                });
            });
            var options = {
                url: ctx + "base/etl/datax/selectByTemplateId",
                method: 'GET', //请求方式
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove?",
                exportUrl: prefix + "/export",
                pagination: false,
                modalName: "定时管理",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'idDrawDatax',
                    title: '主键',
                    visible: false
                },
                {
                    field: 'tbTar',
                    title: '编码'
                },
                {
                    field: 'na',
                    title: '名称'
                },
                {
                    field: 'idDb',
                    title: '数据源'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-danger btn-xs " href="javascript:void(0)" onclick="remove(\'' + row.idDrawDatax + '\',\'datax\')"><i class="fa fa-remove"></i>删除</a> ');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
            $('#dw-table').bootstrapTable({
                url: ctx + "base/etl/dw/selectByTemplateId?templateId="+$("#templateId").val(),
                modalName: "数仓列表",
                showRefresh: true,
                showSearch: true,
                columns: [{
                    checkbox: true
                },
                    {
                        field: 'idDw',
                        title: '主键',
                        visible: false
                    },
                    {
                        field: 'cdTb',
                        title: '表名'
                    },
                    {
                        field: 'na',
                        title: '表描述'
                    },
                    {
                        field: 'fgAct',
                        title: '启用标志'
                    },
                    {
                        field: 'dtSysCre',
                        title: '创建时间'
                    },
                    {
                        title: '操作',
                        align: 'center',
                        formatter: function(value, row, index) {
                            var actions = [];
                            actions.push('<a class="btn btn-danger btn-xs " href="javascript:void(0)" onclick="remove(\'' + row.idDw + '\',\'dw\')"><i class="fa fa-remove"></i>删除</a> ');
                            return actions.join('');
                        }
                    }]
            });
            // $('#dm-table').bootstrapTable({
            //     url: ctx + "base/etl/dm/selectByTemplateId?templateId="+$("#templateId").val(),
            //     modalName: "数仓列表",
            //     showRefresh: true,
            //     showSearch: true,
            //     columns: [{
            //         checkbox: true
            //     },
            //         {
            //             field: 'idDrawDm',
            //             title: '主键',
            //             visible: false
            //         },
            //         {
            //             field: 'cdTb',
            //             title: '表名'
            //         },
            //         {
            //             field: 'idIndex',
            //             title: '指标'
            //         },
            //         {
            //             field: 'naIndex',
            //             title: '指标名称'
            //         },
            //         {
            //             field: 'dtSysCre',
            //             title: '创建时间'
            //         },
            //         {
            //             title: '操作',
            //             align: 'center',
            //             formatter: function(value, row, index) {
            //                 var actions = [];
            //                 actions.push('<a class="btn btn-danger btn-xs " href="javascript:void(0)" onclick="remove(\'' + row.idDrawDm + '\',\'dm\')"><i class="fa fa-remove"></i>删除</a> ');
            //                 return actions.join('');
            //             }
            //         }]
            // });
        });
    </script>
</body>
</html>